package com.cs.common.init;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;

import com.cpi.cornpp.common.filter.BreadCrumbList;
import com.cs.common.init.beans.SiteMapDTO;

/**
 * Sysmte Parameters collection
 * @version 1.0 
 * @date  2008-10-19
 * @author Administrador
 */

public class SiteMapCollection extends HashMap implements IInit {
	/** Serialization UID */
	private static final long serialVersionUID = 2678145409924401663L;
	/** Class instance */
	private static SiteMapCollection sitemapCollection = null;
	/** */
	private HashMap breadcrumbsMap = new HashMap(); 
	/**
	 * Gets class instance filled
	 */
	public static SiteMapCollection getInstance(){
		return sitemapCollection;
	}
	/**
	 * Loads the xml file into class
	 * @param object Object to load
	 */
	public void init(Object object) {
		Collection universo = null; 
		/**Nodo Inicial*/
		SiteMapDTO init = null;
		/** Nodo auxiliar*/
		SiteMapDTO aux = null;
		/** Nodo inmediato superior */
		SiteMapDTO padre = null;
		String name =  null;
		BreadCrumbList breadcrumbs = null;
				
		if( object != null ){
			if( object instanceof SiteMapCollection){
				sitemapCollection = (SiteMapCollection) object;
			}
		}		
		universo = sitemapCollection.values();
		Object[] array = (Object[])universo.toArray();				 
		try{
			for (int i = 0; i < array.length; i++) {			
				init = (SiteMapDTO)array[i];
				name = init.getName();
				breadcrumbs = new  BreadCrumbList(0);
				breadcrumbs.setLabel(init.getLabel());
				breadcrumbs.setSameAs(init.getSameAs());
				/**Agregamos el objeto lista de breadcrumbs al hashmap
				Esta lista esta inicialmente vacia*/ 
				breadcrumbsMap.put(init.getName(), breadcrumbs);
				/**Aqui populamos la lista recien agregada al hashmap iterando de manera ascendente
				el padre de cada nodo hasta que ya no exista uno, al final de cada ciclo 
				la lista tendra los breadcrumbs ancestors del breadcrumb en cuestion*/
				while(!name.trim().equals("")){
					
					aux = (SiteMapDTO)sitemapCollection.get(name);
					if(aux !=null){
						name = aux.getUrlParent();					
						if(!(name.trim().equals(""))){
							padre = (SiteMapDTO)sitemapCollection.get(name);
							breadcrumbs.add(padre);	
						}								
					}
				}
				Collections.reverse(breadcrumbs);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * Add PhonesTypeBean to the collection
	 * @param phonesTypeBean
	 */
	public void addSiteMapDTO( SiteMapDTO SiteMapDTO){
		this.put(SiteMapDTO.getName(), SiteMapDTO);
	}
	
	/**
	 * Gets parameter bean
	 * @param code Parameter code
	 * @return SystemParameterBean
	 */
	public SiteMapDTO getSiteMapDTO(String name){
		SiteMapDTO SiteMapDTO = null;
		SiteMapDTO = (SiteMapDTO)this.get(name);
		return SiteMapDTO;
	}
	
	/**
	 * 
	 * @param name
	 * @return
	 */
	public BreadCrumbList getBreadCrumbsFor(String name){
		return (BreadCrumbList)breadcrumbsMap.get(name);
	}
	 
}